import * as actionTypes from "./actionTypes";
import { fromJS } from "immutable";
// immutable的fromJS是把普通对象转换为immutable对象
// immutable对象的set方法，会结合之前immutable对象的值
// 和设置的值，返回一个全新的对象

const defaultState = fromJS({
    focused: false,
    mouseIn: false,
    list: [],
    page: 1,
    totalPage: 1
});

// 纯函数，给定固定的输入，就一定会有固定的输出，而且不会有任何副作用
export default (state = defaultState, action) => {
    if (action.type === actionTypes.SEARCH_FOCUS) {
        return state.set("focused", true);
    }

    if (action.type === actionTypes.SEARCH_BLUR) {
        return state.set("focused", false);
    }

    if (action.type === actionTypes.MOUSE_ENTER) {
        return state.set("mouseIn", true);
    }

    if (action.type === actionTypes.MOUSE_LEAVE) {
        return state.set("mouseIn", false);
    }

    if (action.type === actionTypes.CHANGE_PAGE) {
        return state.set("page", action.page);
    }

    if (action.type === actionTypes.CHANGE_LIST) {
        return state.merge({
            list: action.data,
            totalPage: action.totalPage
        });
    }

    return state;
};
